package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.domain.vo.DatCount;
import com.ruoyi.system.domain.vo.DeviceCount;
import com.ruoyi.system.domain.vo.IndexVO;
import com.ruoyi.system.domain.vo.WarnCount;
import com.ruoyi.system.mapper.DatInfoMapper;
import com.ruoyi.system.mapper.DeviceMapper;
import com.ruoyi.system.mapper.WarnMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 配件Controller
 *
 * @author wxd
 * @date 2024-08-25
 */
@Api(value = "统计", tags = {"统计"})
@RestController
@RequestMapping("/system/count")
public class CountController extends BaseController {
    @Autowired
    private DatInfoMapper datInfoMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private WarnMapper warnMapper;

    @ApiOperation("首页")
    @GetMapping("/index")
    public R<IndexVO> index(@RequestParam(value = "deptId", required = false) Long deptId,
                            @RequestParam(value = "deviceId", required = false) String deviceId) {
        deviceMapper.expire();
        IndexVO index = new IndexVO();
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = getDateStartDay(now);
        Date endTime = getDateEndDay(now);


        DatCount dayDatCount = datInfoMapper.datCount(deptId, deviceId, sdf.format(startTime), sdf.format(endTime));
        dayDatCount.setCountDateStart(startTime).setCountDateEnd(endTime);
        index.setDayDatCount(dayDatCount);

        WarnCount dayWarnCount = warnMapper.warnCount(deptId, deviceId, sdf.format(startTime), sdf.format(endTime));
        dayWarnCount.setCountDateStart(startTime).setCountDateEnd(endTime);
        index.setDayWarnCount(dayWarnCount);


        Date weekStartTime = calDay(startTime, -7);
        DatCount weekDatCount = datInfoMapper.datCount(deptId, deviceId, sdf.format(weekStartTime), sdf.format(endTime));
        dayDatCount.setCountDateStart(weekStartTime).setCountDateEnd(endTime);
        index.setWeekDatCount(weekDatCount);

        WarnCount weekWarnCount = warnMapper.warnCount(deptId, deviceId, sdf.format(weekStartTime), sdf.format(endTime));
        index.setWeekWarnCount(weekWarnCount);
        weekWarnCount.setCountDateStart(weekStartTime).setCountDateEnd(endTime);


        Date monthStartTime = getDateStartMonth(startTime);
        DatCount monthDatCount = datInfoMapper.datCount(deptId, deviceId, sdf.format(monthStartTime), sdf.format(endTime));
        dayDatCount.setCountDateStart(monthStartTime).setCountDateEnd(endTime);
        index.setMonthDatCount(monthDatCount);

        WarnCount monthWarnCount = warnMapper.warnCount(deptId, deviceId, sdf.format(monthStartTime), sdf.format(endTime));
        index.setMonthWarnCount(monthWarnCount);
        monthWarnCount.setCountDateStart(monthStartTime).setCountDateEnd(endTime);

        List<DatCount> datCountList = new ArrayList<>();
        for (int i = 0; i >= -6; i--) {
            Date st = calMonth(monthStartTime, i);
            Date et = getDateEndMonth(st);
            DatCount count = datInfoMapper.datCount(deptId, deviceId, sdf.format(st), sdf.format(et));
            count.setCountDateStart(st);
            count.setCountDateEnd(et);
            datCountList.add(count);
        }
        index.setDatCountList(datCountList);

        DeviceCount deviceCount = deviceMapper.deviceCount(deptId, deviceId);
        index.setDeviceCount(deviceCount);

        List<DeviceCount> deviceModelCounts = deviceMapper.deviceModelCount(deptId, deviceId);
        index.setDeviceModelCount(deviceModelCounts);

        return R.ok(index);
    }

    private static Date getDateEndDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    private static Date getDateStartDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    private static Date getDateStartMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    private static Date getDateEndMonth(Date date) {
        Date nextM = calMonth(date, 1);
        Date lastDay = calDay(nextM, -1);
        return getDateEndDay(lastDay);
    }

    private static Date calDay(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, day);
        return cal.getTime();
    }

    private static Date calMonth(Date date, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);
        return cal.getTime();
    }

}
